import {Component, OnInit} from '@angular/core';
import 'rxjs/add/operator/switchMap';
import {ProjectStep} from "../entity/ProjectStep";
import {FileUploader} from "ng2-file-upload";
import {ProjectAddService} from "../service/project.add.service";
import {Params, Router} from "@angular/router";
import {ActivatedRoute} from "@angular/router";
import {ProjectFindService} from "../service/project.find.service";
import {Project} from "../entity/Project";
import {CategoryFindService} from "../../category/service/category.find.service";
import {TreeEntity} from "../../shared/entity/TreeEntity";
import {TreeUtils} from "../../shared/utils/TreeUtils";
import {Category} from "../../category/entity/Category";
import {CommonMsg} from "../../shared/entity/CommonMsg";
import {ProjectDeleteService} from "../service/project.delete.service";

declare var $: any;
@Component({selector:'project-settings',
  templateUrl:'./project-settings.html',
  styleUrls: ['./project-index-settings.css']
})
export  class ProjectSetingsComponent
implements OnInit
{
  public editor;
  public project:Project = new Project();
  public categoryList:Array<Category> = new Array<Category>();
  public tree:Array<TreeEntity>;
  public treeUtil:TreeUtils  = new TreeUtils();

  ngOnInit(): void {
    this.categoryFindService.findCategoryTreeData()
      .then(categoryList =>{
        this.tree = this.treeUtil.sonsTree(categoryList,1);

      });
    this.activatedRouter.params
    // (+) converts string 'id' to a number
      .switchMap((params: Params) => this.projectFindService.getProjectInfo(params["id"]))
      .subscribe((project: Project) => this.project = project);
  }
  constructor(private projectAddService: ProjectAddService,
              private projectFindService:ProjectFindService,
              private projectDeleteService:ProjectDeleteService,
              private categoryFindService:CategoryFindService,
              private activatedRouter: ActivatedRoute,
              private router: Router
  ) {


  }


  onSubmit() {

    var projectCategoryId =  $("#categoryId").val();
    this.project.category.id = projectCategoryId;
    this.projectAddService.updateProject(this.project).then(commonMsg=>
      {
        this.handleResult(commonMsg);
      }
    );
  }
  public handleResult(commonMsg:CommonMsg<any>){
    if(commonMsg!=null&&commonMsg.code==200){
      $.jBox.tip("项目修改成功!");

      this.router.navigate(['/project/index/',this.project.id]);


    }else{
      $.jBox.tip("项目修改失败!");
    }
  }
  public deleteProject(id:string){
    let self = this;
    var submit = function (v, h, f) {
      if (v == 'ok'){
        self.deleteProjectService();
      }
      else if (v == 'cancel'){

      }
      return true; //close
    };
    $.jBox.confirm("确定删除项目"+self.project.title+"吗？删除后将不可恢复！", "提示", submit);
  }

  private deleteProjectService() {
    this.projectDeleteService.deleteProject(this.project.id).then(commonMsg=>
      {
        this.handleDeleteProjectResult(commonMsg);
      }
    );
  }
  public handleDeleteProjectResult(commonMsg:CommonMsg<any>){
    if(commonMsg!=null&&commonMsg.code==200){
      $.jBox.tip("删除项目成功!");
      this.project = commonMsg.entity;
      sessionStorage.setItem("personInfoId",sessionStorage.getItem("id"));
      this.router.navigate(['/personal/personalFollow/',{id:sessionStorage.getItem("personInfoId")}]);

    }else{
      $.jBox.tip("删除项目失败!");
    }
  }
  onClickMe(){
    // document.domain = "http://www.jiangxindc.com";
    // 正常打开
    // 用iframe显示http://www.baidu.com的内容，并设置了标题、宽与高、按钮
    //./../category/components/category-tree.html




    $.jBox("id:box", {
      title: "选择分类信息",
      width: 300,
      height: 420,
      buttons:{"确定":"ok","关闭":"cancel"},
      submit:function (v, h, f) {
        if(v=="cancel"){
          $.jBox.tip("cancel");
        }
        if(v=="ok")
        {
          var name = h.find('input[name="category-info"]:checked').val();
          // var name1= h.find('input[name="category-info"]:checked').val();
          // var name2=  h.find('input:radio:checked').val();
          // var name4=  h.find('input[name="category-info"]').filter(':checked');

          var data = name.split(',');
          // this.project.title=data[0];
          $("#categoryName").val(data[0]);
          $("#categoryId").val(data[1]);

        }
      },loaded:function(h){
        $(".jbox-content",top.document).css("overflow-y","hidden");
      }

    });


  }
// Reveal in html:
//  ssalsjdf;aaksjd;fsakdf
//   Name via form.controls = {{showFormControls(heroForm)}}
  showFormControls(form: any) {
    return form && form.controls['name'] &&
      form.controls['name'].value; // Dr. IQ
  }



  public toProjectIndex(id:string){

    this.router.navigate(['/project/index/',id]);
  }


}
